import { memo } from 'react'
import { Navigate } from 'react-router-dom'
import { useSelector } from 'react-redux'
import PropTypes from 'prop-types'

const AuthRoute = memo(({ children }) => {
  const { token } = useSelector(state => state.user)
  return token ? children : <Navigate to="/login" replace />
})

AuthRoute.propTypes = {
  children: PropTypes.node.isRequired
}

AuthRoute.displayName = 'AuthRoute'
export default AuthRoute
